<?php 
/*
 module:		取消保护控制器
 create_time:	2023-10-13 02:55:40
 author:		
 contact:		
*/

namespace app\admin\controller;
use think\exception\ValidateException;
use app\admin\model\Unprotect as UnprotectModel;
use think\facade\Db;
use app\admin\model\Documentary as DocumentaryModel;

class Unprotect extends Admin {


	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'add_time,status,name,mobile,info,sell,area,floor,regoin,contact_address,type,property_type,lease_term,specs,deposit,click,documentary_info,documentary_mothod,connection_type,url,evolve,need_type,weixin,img,back';
		$data = $this->request->only(explode(',',$postField),'post',null);

		$this->validate($data,\app\admin\validate\Unprotect::class);

		$data['add_time'] = time();
		$data['connection_type'] = implode(',',$data['connection_type']);

		try{
			$res = UnprotectModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'resourcse_id,add_time,status,name,mobile,info,sell,area,floor,regoin,contact_address,type,property_type,lease_term,specs,deposit,click,documentary_info,documentary_mothod,connection_type,url,evolve,need_type,weixin,img,back';
		$data = $this->request->only(explode(',',$postField),'post',null);

		$this->validate($data,\app\admin\validate\Unprotect::class);

		$data['add_time'] = !empty($data['add_time']) ? strtotime($data['add_time']) : '';
		$data['connection_type'] = implode(',',$data['connection_type']);

		try{
			UnprotectModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->post('resourcse_id', '', 'serach_in');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'resourcse_id,add_time,status,name,mobile,info,sell,area,floor,regoin,contact_address,type,property_type,lease_term,specs,deposit,click,documentary_info,documentary_mothod,connection_type,url,evolve,need_type,weixin,img,back';
		$res = UnprotectModel::field($field)->find($id);
		$res['connection_type'] = explode(',',$res['connection_type']);
		return json(['status'=>200,'data'=>$res]);
	}



/*start*/
	/*
 	* @Description  数据列表
 	*/
	function index(){
		if (!$this->request->isPost()){
			return view('index');
		}else{
		    $admin = session('admin');
			$limit  = $this->request->post('limit', 20, 'intval');
			$page = $this->request->post('page', 1, 'intval');

    
			$where = [];
			/*
			if($this->request->post('type') == 1){
			    $where['type'] = ['like','餐饮'];
			}
			*/
			$where['resourcse_id'] = $this->request->post('resourcse_id', '', 'serach_in');
			//$where['resourcse_id'] = $this->request->post('resourcse_id', '', 'serach_in');

			$add_time = $this->request->post('add_time', '', 'serach_in');
			$where['add_time'] = ['between',[strtotime($add_time[0]),strtotime($add_time[1])]];
			$where['name'] = ['like',$this->request->post('name', '', 'serach_in')];
			$where['mobile'] = ['like',$this->request->post('mobile', '', 'serach_in')];
			$where['auto_mobile'] = ['like',$this->request->post('auto_mobile', '', 'serach_in')];
			$where['info'] = ['like',$this->request->post('info', '', 'serach_in')];
			$where['regoin'] = ['like',$this->request->post('regoin','','serach_in')];
			$where['type'] = ['like',$this->request->post('type','','serach_in')];
			$where['sell'] = ['like',$this->request->post('sell', '', 'serach_in')];
			$where['documentary_mothod'] = $this->request->post('documentary_mothod', '', 'serach_in');
			$where['connection_type'] = ['find in set',$this->request->post('connection_type', '', 'serach_in')];
			$where['status'] = $this->request->post('status', '', 'serach_in');
			$where['evolve'] = $this->request->post('evolve', '', 'serach_in');
			$where['weixin'] = $this->request->post('weixin', '', 'serach_in');
            //$where['status'] = ['<>',3];

            if($where['mobile'][1]){
                $page = 1;
            }

			$field = 'resourcse_id,add_time,name,mobile,info,sell,click,doucmentary_num,documentary_info,documentary_mothod,status,evolve,save_display,regoin,type,auto_mobile,mobile_repeat_bh';

			$order  = $this->request->post('order', '', 'serach_in');	//排序字段
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

			$orderby = ($sort && $order) ? $sort.' '.$order : 'resourcse_id desc';

			$query = UnprotectModel::field($field);

			$res =$query->where(formatWhere($where))->where('status','<>',3)->where('unprotect','=',1)->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
    
            foreach($res['data'] as $k => $v){
                
                if(strpos($v['type'],'餐饮美食') !== false){
                    $res['data'][$k]['type'] = '餐饮美食';
                }elseif(strpos($v['type'],'美容美发') !== false){
                    $res['data'][$k]['type'] = '美容美发';
                }elseif(strpos($v['type'],'服饰鞋包') !== false){
                    $res['data'][$k]['type'] = '服饰鞋包';
                }elseif(strpos($v['type'],'休闲娱乐') !== false){
                    $res['data'][$k]['type'] = '休闲娱乐';
                }elseif(strpos($v['type'],'百货超市') !== false){
                    $res['data'][$k]['type'] = '百货超市';
                }elseif(strpos($v['type'],'生活服务') !== false){
                    $res['data'][$k]['type'] = '生活服务';
                }elseif(strpos($v['type'],'电器通讯') !== false){
                    $res['data'][$k]['type'] = '电器通讯';
                }elseif(strpos($v['type'],'汽修美容') !== false){
                    $res['data'][$k]['type'] = '汽修美容';
                }elseif(strpos($v['type'],'医疗器械') !== false){
                    $res['data'][$k]['type'] = '医疗器械';
                }elseif(strpos($v['type'],'家居建材') !== false){
                    $res['data'][$k]['type'] = '家居建材';
                }elseif(strpos($v['type'],'教育培训') !== false){
                    $res['data'][$k]['type'] = '教育培训';
                }elseif(strpos($v['type'],'酒店宾馆') !== false){
                    $res['data'][$k]['type'] = '酒店宾馆';
                }elseif(strpos($v['type'],'其他') !== false){
                    $res['data'][$k]['type'] = '其他';
                }
                
                if($v['save_display']){
                    $saveDisplay = explode(',',$v['save_display']);
                    if(in_array($admin['user_id'],$saveDisplay)){
                        $res['data'][$k]['save_display'] = 1;
                    }else{
                        $res['data'][$k]['save_display'] = 0;
                    }
                }else{
                    $res['data'][$k]['save_display'] = 0;
                }
            }
            

			$data['status'] = 200;
			$data['data'] = $res;
			return json($data);
		}
	}
/*end*/

 /*start*/

    function md5s(){
        $pass =  $this->request->post('pass', '', 'serach_in');
        $pass = md5($pass);
        return json(['status'=>200,'data'=>$pass]);
    }

    function protect(){
        $admin = session('admin');
        if (!$this->request->isPost()){
            return view('index');
        }else{
            $limit  = $this->request->post('limit', 20, 'intval');
            $page = $this->request->post('page', 1, 'intval');

            $where = [];
            $where['resourcse_id'] = $this->request->post('resourcse_id', '', 'serach_in');
            $where['resourcse_id'] = $this->request->post('resourcse_id', '', 'serach_in');

            $add_time = $this->request->post('add_time', '', 'serach_in');
            $where['add_time'] = ['between',[strtotime($add_time[0]),strtotime($add_time[1])]];
            $where['name'] = ['like',$this->request->post('name', '', 'serach_in')];
            $where['mobile'] = $this->request->post('mobile', '', 'serach_in');
            $where['documentary_mothod'] = $this->request->post('documentary_mothod', '', 'serach_in');
            $where['connection_type'] = ['find in set',$this->request->post('connection_type', '', 'serach_in')];
            $where['status'] = $this->request->post('status', '', 'serach_in');
            $where['evolve'] = $this->request->post('evolve', '', 'serach_in');
            $where['weixin'] = $this->request->post('weixin', '', 'serach_in');
            $where['protect_user_id'] = $admin['user_id'];
            $where['status'] = ['<>',3];

            $field = 'resourcse_id,add_time,name,mobile,info,sell,click,doucmentary_num,documentary_info,documentary_mothod,status,evolve';

            $order  = $this->request->post('order', '', 'serach_in');	//排序字段
            $sort  = $this->request->post('sort', '', 'serach_in');		//排序方式

            $orderby = ($sort && $order) ? $sort.' '.$order : 'resourcse_id desc';

            $query = UnprotectModel::field($field);

            $res =$query->where(formatWhere($where))->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

            $data['status'] = 200;
            $data['data'] = $res;
            return json($data);
        }
    }
/*end*/


/*start*/
    function detail(){
        $admin = session('admin');
        $id =  $this->request->post('resourcse_id', '', 'serach_in');
        if(!$id) throw new ValidateException ('参数错误');
        $field = 'resourcse_id,add_time,name,mobile,info,sell,area,floor,regoin,contact_address,type,property_type,lease_term,specs,deposit,click,documentary_mothod,connection_type,url,evolve,need_type,weixin,img,back';
        $res = UnprotectModel::field($field)->findOrEmpty($id);

        $res['connection_type'] = explode(',',$res['connection_type']);

        if($res->isEmpty()){
            throw new ValidateException ('信息不存在');
        }
        $resource = Db::name("resource")->where('resourcse_id',$id)->find();
        
        //$field = 'id,resourcse_id,documentary_info,contacts,add_time';
        //$query = DocumentaryModel::field($field);
        
        $documentary = Db::name('documentary')->where('resourcse_id',$id)->where('documentary_info','<>','')->select()->toArray();
        
        if(in_array(1,$res['connection_type'])) {
            $admin = Db::name("admin_user")->where('user_id', $resource['protect_user_id'])->find();
            $res['user_name'] = $admin['name'];
        }
        $data = array();
        $data['resourcse_id'] = $id;
        $data['click'] = $resource['click'] + 1;

        try{
            UnprotectModel::update($data);
        }catch(\Exception $e){
            throw new ValidateException($e->getMessage());
        }
        $userCall = array();
        if($admin['user_id'] == 34){
            $userCall['username'] = '102879801';
            $userCall['passMd5'] = md5(971483);
        }elseif($admin['user_id'] == 1){
            $userCall['username'] = '102879800';
            $userCall['passMd5'] = md5(87438394);
        }elseif($admin['user_id'] == 35){
            $userCall['username'] = '102879802';
            $userCall['passMd5'] = md5(372081);
        }
        return json(['status'=>200,'data'=>$res,'user_call'=>$userCall,'documentary'=>$documentary]);
    }
/*end*/


/*start*/
    public function documentary(){
        $admin = session('admin');

        $postField = 'resourcse_id,add_time,name,mobile,info,sell,area,floor,regoin,contact_address,type,property_type,lease_term,specs,deposit,click,documentary_info,documentary_mothod,connection_type,url,evolve,need_type,weixin,img,back';
        $data = $this->request->only(explode(',',$postField),'post',null);

        $documentaryData = array();
        $documentaryData['resourcse_id'] = $data['resourcse_id'];
        $documentaryData['documentary_mothod'] = $data['documentary_mothod'];
        $documentaryData['documentary_info'] = $data['documentary_info'];
        $documentaryData['evolve'] = $data['evolve'];
        $documentaryData['contacts'] = $admin['name'];
        $documentaryData['user_id'] = $admin['user_id'];
        $documentaryData['add_time'] = time();

        try{
            $res = DocumentaryModel::insertGetId($documentaryData);
        }catch(\Exception $e){
            throw new ValidateException($e->getMessage());
        }
        

        $this->validate($data,\app\admin\validate\Resource::class);

        //$data['add_time'] = !empty($data['add_time']) ? strtotime($data['add_time']) : '';
        $mobileRepeat = Db::name("resource")->whereRaw("mobile = '{$data['mobile']}' or auto_mobile = '{$data['mobile']}'")->field('resourcse_id')->select()->toArray();
        $mobileRepeatArr = array();
        
        foreach($mobileRepeat as $v){
            $mobileRepeatArr[] = $v['resourcse_id'];
        }
        
        $data['mobile_repeat_bh'] = implode(',',$mobileRepeatArr);
    
        $resource = Db::name("resource")->where('resourcse_id',$data['resourcse_id'])->find();
        $data['protect_time'] = time()+172800;
        
    
        if($resource['protect_user_id'] > 0){
            
            if($resource['protect_user_id'] == $admin['user_id']){
    
                if(!$data['connection_type'][0]){
    
                    $data['protect_user_id'] = 0;
                    $data['unprotect'] = 1;
                    $data['protect_time'] = NULL;
                    
                }
            }else {
    
                $data['connection_type'] = array(1);
            }
        }else{
            if(in_array(1,$data['connection_type'])){
                
                $count = Db::name("resource")->where('protect_user_id',$admin['user_id'])->count();
            
                if($count >= 8){
                    return json(['status'=>301,'msg'=>'最多只能保护8位客户，请取消其他保护客户']);
                }
            
                $data['protect_user_id'] = $admin['user_id'];
                $data['unprotect'] = 0;
                
            }
        }
        
    
        $data['connection_type'] = implode(',',$data['connection_type']);
        $data['doucmentary_num'] = $resource['doucmentary_num'] + 1;

        if($resource['save_display']) {
            $saveDisplay = explode(',', $resource['save_display']);
            if (!in_array($admin['user_id'], $saveDisplay)) {
                $saveDisplay[] = $admin['user_id'];
            }
        }else{
            $saveDisplay[] = $admin['user_id'];
        }

        $data['save_display'] = implode(',',$saveDisplay);

        try{
            ResourceModel::update($data);
        }catch(\Exception $e){
            throw new ValidateException($e->getMessage());
        }

        return json(['status'=>200,'msg'=>'保存成功']);


    }
    /*end*/




}

